using System.Collections.Generic;
using System.Text;
using OracleInternal.Common;

namespace OracleInternal.SqlAndPlsqlParser.LocalParsing
{
	internal class OracleLpQteNamedObject : OracleLpQueryTableExpression
	{
		protected OracleLpSubqueryFactoringTerm m_vSubqueryFactoringTerm;

		protected OracleLpName m_vObjectName;

		protected OracleLpName m_vSchemaName;

		protected OracleLpDbLink m_vDblink;

		public object SubqueryFactoringTerm => m_vSubqueryFactoringTerm;

		public override OracleLpQueryTableExpressionType QueryTableExpressionType => OracleLpQueryTableExpressionType.NamedObject;

		public virtual OracleLpName ObjectName
		{
			get
			{
				return m_vObjectName;
			}
			set
			{
				m_vObjectName = value;
			}
		}

		public OracleLpName SchemaName
		{
			get
			{
				return m_vSchemaName;
			}
			set
			{
				m_vSchemaName = value;
			}
		}

		public OracleLpDbLink Dblink
		{
			get
			{
				return m_vDblink;
			}
			set
			{
				m_vDblink = value;
			}
		}

		public OracleLpQteNamedObject(OracleLpStatementElement tr)
			: base(tr)
		{
		}

		public override void Resolve()
		{
			if (m_vSubqueryFactoringTerm != null)
			{
				m_vColumnDescriptors = m_vSubqueryFactoringTerm.ColumnDescriptors;
				return;
			}
			m_vColumnDescriptors = new List<OracleLpColumnDescriptor>();
			for (int i = 1; i <= 5; i++)
			{
				OracleLpColumnDescriptor oracleLpColumnDescriptor = new OracleLpColumnDescriptor();
				oracleLpColumnDescriptor.ColumnName = new OracleLpName($"{m_vObjectName.DbName}_col{i}");
				oracleLpColumnDescriptor.BaseTableName = m_vObjectName;
				oracleLpColumnDescriptor.BaseSchemaName = m_vSchemaName;
				oracleLpColumnDescriptor.BaseColumnName = oracleLpColumnDescriptor.ColumnName;
				m_vColumnDescriptors.Add(oracleLpColumnDescriptor);
			}
		}

		public override void RetrieveNamedObjectReferences(OracleLpStatement statement)
		{
			if (m_vSchemaName == null && m_vDblink == null)
			{
				for (OracleLpQueryBlock parentQueryBlock = GetParentQueryBlock(); parentQueryBlock != null; parentQueryBlock = parentQueryBlock.GetParentQueryBlock())
				{
					if (parentQueryBlock.WithClause != null && parentQueryBlock.WithClause.SubqueryFactoringClause != null)
					{
						List<OracleLpSubqueryFactoringTerm> subqueryFactoringTerms = parentQueryBlock.WithClause.SubqueryFactoringClause.SubqueryFactoringTerms;
						if (subqueryFactoringTerms != null)
						{
							foreach (OracleLpSubqueryFactoringTerm item in subqueryFactoringTerms)
							{
								if (item.ColumnMappedQueryName.Name.DbName == m_vObjectName.DbName)
								{
									m_vSubqueryFactoringTerm = item;
									break;
								}
							}
						}
					}
					if (m_vSubqueryFactoringTerm != null)
					{
						break;
					}
				}
			}
			if (m_vSubqueryFactoringTerm != null)
			{
				m_vSubqueryFactoringTerm.RetrieveNamedObjectReferences(statement);
			}
			else
			{
				statement.NamedObjectsReferences.Add(this);
			}
		}

		public void RetrieveColumnsInformation(OracleLpTableColumns tabCols)
		{
			m_vColumnDescriptors = new List<OracleLpColumnDescriptor>();
			foreach (OracleInternal.Common.OracleLpColumn column in tabCols.m_columns)
			{
				OracleLpColumnDescriptor oracleLpColumnDescriptor = new OracleLpColumnDescriptor();
				oracleLpColumnDescriptor.BaseSchemaName = new OracleLpName();
				oracleLpColumnDescriptor.BaseSchemaName.DbName = column.m_schemaName;
				oracleLpColumnDescriptor.BaseTableName = new OracleLpName();
				oracleLpColumnDescriptor.BaseTableName.DbName = column.m_tableName;
				OracleLpName oracleLpName = new OracleLpName();
				oracleLpName.DbName = column.m_columnName;
				OracleLpName oracleLpName4 = (oracleLpColumnDescriptor.ColumnName = (oracleLpColumnDescriptor.BaseColumnName = oracleLpName));
				oracleLpColumnDescriptor.ColumnType = OracleLpColumnType.Column;
				oracleLpColumnDescriptor.IsHidden = column.m_isHidden;
				m_vColumnDescriptors.Add(oracleLpColumnDescriptor);
			}
		}

		internal override void ToString(StringBuilder sb)
		{
			string depthIndent = base.DepthIndent;
			base.ToString(sb);
			sb.Append(depthIndent);
			sb.Append("  Name: ");
			sb.Append(m_vObjectName);
			sb.Append("  Schema: ");
			sb.Append((m_vSchemaName == null) ? "none" : (m_vSchemaName.DbName ?? "none"));
			sb.Append('\n');
		}
	}
}
